<?php
class Credit_model extends CI_Model {

	/**
	 * @author Pwint Phyu Shwe
	 * Credit Model for tbl_credit
	 * add and deduct credit for user.
	 * Refer detail in each function's comment section below.
	 */
	// table name
	private $tbl_usercredit = 'userpoints';

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}
	
	
	// get person by userID
	function get_by_id($userID){
		$this->db->where('userID', $userID);
		return $this->db->get($this->tbl_usercredit);
	}
	
	function add($userID, $points){
		$data = array (
				'userID' => $userID,
				'points' => $points
				);
		$this->db->insert($this->tbl_usercredit, $data);
		return $this->db->insert_id();
	}
	// increase credit
	function increase($userID, $points){
	
		$this->db->where('userID', $userID);
		$this->db->set('points',  'points + ' . (int) $points,FALSE);
		
		return $this->db->update($this->tbl_usercredit);
	}
	// update person by userID
	function decrease($userID, $points){
	
		$this->db->where('userID', $userID);
		$this->db->set('points',  'points - ' . (int) $points,FALSE);
		return $this->db->update($this->tbl_usercredit);
	}
	// set point to zero
	function setzero ($userID){
		$credit = array(
				'points' => 0
		);
		$this->db->where('userID', $userID);
		return $this->db->update($this->tbl_usercredit, $credit);
	}
	
	//get points
	function get_points($userID){
		$this->db->where('userID', $userID);
		$this->db->select('points');
		$query = $this->db->get($this->tbl_usercredit);
		foreach ($query->result() as $row)
		{
			$points =  $row->points;
		}
		return $points;
		
	}
	// check member's credit level
	function check_membercredit($advType, $userID){
		
		$points = $this->get_points($userID);
	
		switch ($advType){
			case 1:
				if ($points >= 30) return TRUE; break;
			case 2:
				if ($points >= 50) return TRUE; break;
			case 3:
				if ($points >= 70) return TRUE; break;
				defaut:
				return false; break;
		}
	
	}
}
?>